package com.bitter.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;

import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2
@EnableKnife4j
@ConditionalOnExpression("${knife4j.enable}") //开启访问接口文档的权限  **knife4j.enable是在yml配置文件中配置为true**
public class SwaggerConfiguration {


    public SwaggerConfiguration(Environment environment, OpenApiExtensionResolver openApiExtensionResolver) {
        this.environment = environment;
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    private Environment environment;

    @Bean
    public Docket createRestApi() {
        // get env enable val
        boolean enable = environment.acceptsProfiles(Profiles.of("dev", "test"));
        return new Docket(DocumentationType.OAS_30).enable(enable).apiInfo(apiInfo()).groupName("1.0").select().apis(RequestHandlerSelectors.basePackage("com.bitter.controller")).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Spring Boot中使用Swagger3构建RESTful APIs(带认证）").description("更多Spring Boot相关文章请关注：https://blog.csdn.net/elvishehai").termsOfServiceUrl("https://blog.csdn.net/elvishehai").version("1.0").build();
    }

    /*引入Knife4j提供的扩展类*/
    private OpenApiExtensionResolver openApiExtensionResolver;
}
